<template>
  <amis :amisjson="pageJson"></amis>
</template>
<script setup lang="ts">
import { menuDetail } from "@/api/menu";
import Amis from "@/components/AmisComponent.vue";
import { ref, watch } from "vue";
import { useRoute } from "vue-router";

const route = useRoute();
defineProps<{ 'pageJson': Object }>()
const pageJson = ref({});


function getRouteByPath(path: string) {
  let rs = localStorage.getItem('menuList')
  let arr = [] as any
  if (rs) {
    let r = JSON.parse(localStorage.getItem('menuList') as string)
    for (let i = 0; i < r.length; i++) {
      if (r[i].children && r[i].children.length > 0) {
        _(r[i].children)
      } else {
        arr.push(r[i])
      }
    }
  }
  function _(oArr: any) {
    for (let i = 0; i < oArr.length; i++) {
      if (oArr[i].children && oArr[i].children.length > 0) {
        _(oArr[i].children)
      } else {
        arr.push(oArr[i])
      }
    }
  }
  let p = ''
  arr.forEach((item: any) => {
    if (item.path === path) {
      p = item
    }
  })

  return p
}

watch(() => route, () => {
  let rs = getRouteByPath(route.path) as any
  if (rs) {
    menuDetail(rs.id as string).then(res => {
      pageJson.value = res.data.data
    })
  }

}, {
  immediate: true,
  deep: true,
})

</script>
